/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.unice.miage.oobdoo.servlet;

import com.unice.miage.oobdoo.entity.Album;
import com.unice.miage.oobdoo.entity.Note;
import com.unice.miage.oobdoo.entity.Utilisateur;
import com.unice.miage.oobdoo.manager.AlbumManager;
import com.unice.miage.oobdoo.manager.NoteManager;
import com.unice.miage.oobdoo.manager.UtilisateurManager;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Remi
 */
@WebServlet(name = "RatingServlet", urlPatterns = {"/RatingServlet"})
public class RatingServlet extends HttpServlet {

    @EJB
    private NoteManager noteManager;
    @EJB
    private UtilisateurManager utilisateurManager;
    @EJB
    private AlbumManager albumManager;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String action = request.getParameter("action");
        if (action != null) {
            if (action.equals("get")) {
                int idAlbum = Integer.valueOf(request.getParameter("ia"));
                Album a = albumManager.getAlbumById(idAlbum);
                float globalScore = albumManager.getGlobalScore(a);

                // RECUPERATION DE L'UTILISATEUR
                HttpSession session = request.getSession(true);
                int id = Integer.valueOf(session.getAttribute("id").toString());
                Utilisateur u = utilisateurManager.getUtilisateurById(id);

                Note n = noteManager.getNoteByAlbumAndUser(a, u);
                if (n == null) {
                    out.write("{\"ret\":\"1\","
                            + "\"score\":\"" + globalScore + "\","
                            + "\"me\":\"0.0\""
                            + "}");
                } else {
                    out.write("{\"ret\":\"1\","
                            + "\"score\":\"" + globalScore + "\","
                            + "\"me\":\"" + n.getValue() + "\""
                            + "}");
                }
            } else if (action.equals("submit")) {
                int idAlbum = Integer.valueOf(request.getParameter("ia"));
                Album a = albumManager.getAlbumById(idAlbum);

                HttpSession session = request.getSession(true);
                int id = Integer.valueOf(session.getAttribute("id").toString());
                Utilisateur u = utilisateurManager.getUtilisateurById(id);

                int score = Integer.valueOf(request.getParameter("s"));

                // VERIF SI L'UTILISATERU A DEJA VOTER SI OUI ON UPDATE

                Note n = noteManager.getNoteByAlbumAndUser(a, u);

                if (n != null) {
                    noteManager.updateNote(n, score);
                } else {
                    Note newNote = noteManager.creerNote(u, a, score);
                    albumManager.addNote(a, newNote);
                }

                a = albumManager.getAlbumById(idAlbum);

                float globalScore = albumManager.getGlobalScore(a);


                out.write("{\"ret\":\"1\","
                        + "\"score\":\"" + globalScore + "\","
                        + "\"me\":\"" + score + "\""
                        + "}");

            } else if (action.equals("delete")) {
            }
        }
        out.close();

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
